﻿using BootstrapBlazor.Components;
using FreeSql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BBCore.Application.Extensions;

namespace BBCore.Application
{
    /// <summary>
    /// 全功能数据服务
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    public class FreesqlDataService<TModel> : DataServiceBase<TModel> where TModel : class, new()    
    {
        private readonly IFreeSql _db = BaseEntity.Orm;

        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public override async Task<bool> DeleteAsync(IEnumerable<TModel> models)
        {
            // 通过模型获取主键列数据
            // 支持批量删除
            await _db.Delete<TModel>(models).ExecuteAffrowsAsync();
            return true;
        }

        /// <summary>
        /// 保存方法
        /// </summary>
        /// <param name="model"></param>
        /// <param name="changedType"></param>
        /// <returns></returns>
        public override async Task<bool> SaveAsync(TModel model, ItemChangedType changedType)
        {
            await _db.GetRepository<TModel>().InsertOrUpdateAsync(model);
            return true;
        }

        /// <summary>
        /// 查询方法
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        //public override Task<QueryData<TModel>> QueryAsync(QueryPageOptions option)
        //{
        //    var select = _db.Select<TModel>().WhereDynamicFilter(option.ToDynamicFilter())
        //        .OrderByPropertyNameIf(option.SortOrder != SortOrder.Unset, option.SortName,
        //            option.SortOrder == SortOrder.Asc)
        //        .Count(out var count);
        //    if (option.IsPage)
        //    {
        //        select = select.Page(option.PageIndex, option.PageItems);
        //    }
        //    var Items = select.ToList();
        //    var ret = new QueryData<TModel>()
        //    {
        //        TotalCount = (int)count,
        //        Items = Items,
        //        IsSorted = option.SortOrder != SortOrder.Unset,
        //        IsFiltered = option.Filters.Any(),
        //        IsAdvanceSearch = option.AdvanceSearches.Any(),
        //        IsSearch = option.Searches.Any() || option.CustomerSearches.Any()
        //    };
        //    return Task.FromResult(ret);
        //}

        public override Task<QueryData<TModel>> QueryAsync(QueryPageOptions option)
        {          
            var ret = new QueryData<TModel>()
            {
                IsSorted = option.SortOrder != SortOrder.Unset,
                IsFiltered = option.Filters.Any(),
                IsAdvanceSearch = option.AdvanceSearches.Any(),
                IsSearch = option.Searches.Any() || option.CustomerSearches.Any(),
                Items = _db.Select<TModel>()
                           .WhereDynamicFilter(option.ToDynamicFilter())
                           .OrderByPropertyNameIf(option.SortOrder != SortOrder.Unset, option.SortName, option.SortOrder == SortOrder.Asc)
                           .Count(out var count)
                           .PageIf(option.PageIndex, option.PageItems, option.IsPage)
                           .ToList(),
                TotalCount = Convert.ToInt32(count)
            };
            return Task.FromResult(ret);
        }
    }
}
